package force;

import backbone.IForceable;
import java.awt.Point;
import java.awt.geom.Point2D;
import main.Simulation;

/**
 * Creates a wall repulsion force.
 * @author Paul
 * @author Lance
 */
public class WallRepulsion extends InversePowerForce {
    
    /**
     * int representation of the different sides of the wall.
     */
    public static final int TOP = 1;
    public static final int RIGHT = 2;
    public static final int BOTTOM = 3;
    public static final int LEFT = 4;
    
    private static final String NAME = "Wall";
    
    private Simulation mySimulation;
    private int myID;
    
    /**
     * 
     * @param id The specifier of the wall. TOP = 1, etc...
     * @param scaleFactor The exponent for the inverse power force.
     * @param magnitude 
     * @param sim 
     */
    public WallRepulsion(int id, double scaleFactor,
            double magnitude, Simulation sim) {
        super(scaleFactor, magnitude, NAME + id);
        myID = id;
        mySimulation = sim;
    }
    
    /**
     * 
     * @param wallID
     * @param forceable
     * @return
     */
    private Force computeWallForce(int wallID, IForceable forceable) {
        Point2D pt = new Point();
        if (wallID == TOP) {
            //potential bug
            pt.setLocation(forceable.getPosition().getX(), 0);
        }
        else if (wallID == RIGHT) {
            pt.setLocation(forceable.getPosition().getY(),
                    mySimulation.getSize().getWidth());
        }
        else if (wallID == BOTTOM) {
            pt.setLocation(forceable.getPosition().getY(),
                    mySimulation.getSize().getHeight());
        }
        else if (wallID == LEFT) {
            pt.setLocation(forceable.getPosition().getY(), 0);
        }
        return computeInverseForce(pt, forceable.getPosition());
    }
    
    /**
     * Changes the wall between side walls and top walls.
     * @param id
     */
    public void setID (int id) {
        myID = id;
    }
    
    @Override
    public void update(Simulation canvas, double dt) {
        for (IForceable forceable : getForceables()) {
            Force f = new Force();
            f.sum(computeWallForce(myID, forceable));
            forceable.applyForce(f);
        }        
    }    
}
